//
//  CourseStudyDateView.m
//  ShowYork
//
//  Created by hxh on 2020/9/3.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonStudyDateView.h"

@interface LessonStudyDateView ()
@property (nonatomic,strong)UIImageView *coverImgView;
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIView *lineView;
@property (nonatomic,strong)UIView *leftLine;
@property (nonatomic,strong)UIView *rightLine;
@property (nonatomic,strong)UIView *nowCoverView;
@property (nonatomic,strong)UIView *continueView;
@property (nonatomic,strong)UIView *totalView;
@property (nonatomic,strong)UILabel *nowTitleLb;
@property (nonatomic,strong)UILabel *continueTitleLb;
@property (nonatomic,strong)UILabel *totalTitleLb;

@property (nonatomic,strong)UILabel *nowContentLb;
@property (nonatomic,strong)UILabel *continueContentLb;
@property (nonatomic,strong)UILabel *totalContentLb;

@property (nonatomic,strong)UIView *shadowView;

@property (nonatomic,strong)UILabel *titleLb;

@end

@implementation LessonStudyDateView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, 234);
    CGFloat maxWidth = kScreenW - 32;
    CGFloat avWidth = maxWidth/3;
    
    self.coverImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_study_cover"]];
    [self addSubview:self.coverImgView];
    [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(@200);
    }];
    
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(32);
        make.left.right.bottom.equalTo(self);
    }];
    
    
    self.shadowView = [[UIView alloc]init];
    [self addSubview:self.shadowView];
    
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-16);
        make.height.mas_equalTo(110);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(maxWidth);
    }];
    

    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self.shadowView addSubview:self.containView];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.shadowView);
    }];
    
    self.leftLine = [[UIView alloc]init];
    self.leftLine.backgroundColor = [UIColor lightGrayColor];
    [self.containView addSubview:self.leftLine];

    [self.leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.containView);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(30);
        make.left.equalTo(self.containView).offset(avWidth);
    }];

    self.rightLine = [[UIView alloc]init];
    self.rightLine.backgroundColor = [UIColor lightGrayColor];
    [self.containView addSubview:self.rightLine];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.containView);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(30);
        make.left.equalTo(self.containView).offset(avWidth * 2);
    }];
    
//    [self.shadowView layoutIfNeeded];
    [self.containView layoutIfNeeded];

    
    self.shadowView.layer.shadowColor   = [UIColor lightGrayColor].CGColor;
    self.shadowView.layer.shadowOffset  = CGSizeMake(0, 0);
    self.shadowView.layer.shadowOpacity = 0.8;
    self.shadowView.layer.shadowRadius  = 1;

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.containView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.containView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.containView.layer.mask = maskLayer;
    
    self.nowCoverView = [[UIView alloc]init];
    [self.containView addSubview:self.nowCoverView];
    
    self.continueView = [[UIView alloc]init];
    [self.containView addSubview:self.continueView];
    
    self.totalView = [[UIView alloc]init];
    [self.containView addSubview:self.totalView];
    
    [self.nowCoverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.containView);
        make.width.mas_equalTo(avWidth);
    }];
    
    [self.continueView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.bottom.equalTo(self.containView);
        make.left.equalTo(self.nowCoverView.mas_right);
         make.width.mas_equalTo(avWidth);
     }];
     
    [self.totalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.containView);
         make.width.mas_equalTo(avWidth);
     }];
    
    self.nowTitleLb = [[UILabel alloc]init];
    self.nowTitleLb.text = @"今日学习";
    self.nowTitleLb.textColor = RGBA(102, 102, 102, 1);
    self.nowTitleLb.font = font(12);
    [self.nowCoverView addSubview:self.nowTitleLb];
    
    [self.nowTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.nowCoverView).offset(-22);
        make.centerX.equalTo(self.nowCoverView);
    }];
    self.continueTitleLb = [[UILabel alloc]init];
    self.continueTitleLb.text = @"连续学习";
    self.continueTitleLb.textColor = RGBA(102, 102, 102, 1);
    self.continueTitleLb.font = font(12);
    [self.continueView addSubview:self.continueTitleLb];
    [self.continueTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.continueView).offset(-22);
        make.centerX.equalTo(self.continueView);
    }];
    
    self.totalTitleLb = [[UILabel alloc]init];
    self.totalTitleLb.text = @"累计学习";
    self.totalTitleLb.textColor = RGBA(102, 102, 102, 1);
    self.totalTitleLb.font = font(12);
    [self.totalView addSubview:self.totalTitleLb];
    [self.totalTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.totalView).offset(-22);
        make.centerX.equalTo(self.totalView);
    }];
    
    
    self.nowContentLb = [[UILabel alloc]init];
    self.nowContentLb.textColor = RGBA(35, 24, 21, 1);
//    self.nowContentLb.font = [UIFont boldSystemFontOfSize:32];
    [self.nowCoverView addSubview:self.nowContentLb];
    
    [self.nowContentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.nowCoverView);
        make.bottom.equalTo(self.nowTitleLb.mas_top).offset(-5);
    }];

    
    self.continueContentLb = [[UILabel alloc]init];
    self.continueContentLb.textColor = RGBA(35, 24, 21, 1);
    //    self.nowContentLb.font = [UIFont boldSystemFontOfSize:32];
    [self.continueView addSubview:self.continueContentLb];
        
    [self.continueContentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.continueView);
        make.bottom.equalTo(self.continueTitleLb.mas_top).offset(-5);
    }];
        
    
//    NSMutableAttributedString *attr1 = [[NSMutableAttributedString alloc]initWithString:self.continueContentLb.text];
//
//    [attr1 setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:32]} range:NSMakeRange(0, self.continueContentLb.text.length - 1)];
//    [attr1 setAttributes:@{NSForegroundColorAttributeName:RGBA(102, 102, 102, 1),NSFontAttributeName:font(12)} range:NSMakeRange(self.continueContentLb.text.length - 1, 1)];
//    self.continueContentLb.attributedText = attr1;
//
    
    self.totalContentLb = [[UILabel alloc]init];
    self.totalContentLb.text = @"10年24时60分";
    self.totalContentLb.textColor = RGBA(35, 24, 21, 1);
    [self.totalView addSubview:self.totalContentLb];
            
    [self.totalContentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.totalView);
        make.bottom.equalTo(self.totalTitleLb.mas_top).offset(-5);
    }];
}
- (void)updateDateAlpha:(CGFloat)alpha{
    self.alpha = alpha;
}

- (void)updateData:(LessonUseageModel *)model{
   
    self.continueContentLb.text = [NSString stringWithFormat:@"%d天",model.continuousTime.intValue];
    NSMutableAttributedString *attr1 = [[NSMutableAttributedString alloc]initWithString:self.continueContentLb.text];
    [attr1 setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:32]} range:NSMakeRange(0, self.continueContentLb.text.length - 1)];
    [attr1 setAttributes:@{NSForegroundColorAttributeName:RGBA(102, 102, 102, 1),NSFontAttributeName:font(12)} range:NSMakeRange(self.continueContentLb.text.length - 1, 1)];
    self.continueContentLb.attributedText = attr1;
    
    NSInteger todayTime = model.todayTime.intValue/1000/60;
    self.nowContentLb.text = [NSString stringWithFormat:@"%ld分",(long)todayTime];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.nowContentLb.text];
    [attr setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:32]} range:NSMakeRange(0, self.nowContentLb.text.length - 1)];
    [attr setAttributes:@{NSForegroundColorAttributeName:RGBA(102, 102, 102, 1),NSFontAttributeName:font(12)} range:NSMakeRange(self.nowContentLb.text.length - 1, 1)];
    self.nowContentLb.attributedText = attr;
    
    
    NSInteger index  =  model.allTime.longValue/1000/60;
    
    NSInteger hour = index/60;
    NSInteger minute = index%60;
    
    NSInteger rs_hour = 0;
    NSInteger rs_minute = 0;

    if (hour == 0) {
        rs_minute = minute;
        self.totalContentLb.text = [NSString stringWithFormat:@"%ld分",(long)minute];
    }else{
        rs_hour = hour;
        self.totalContentLb.text = [NSString stringWithFormat:@"%ld时",(long)hour];
    }
    
    NSMutableAttributedString *attr3 = [[NSMutableAttributedString alloc]initWithString:self.totalContentLb.text];
    [attr3 setAttributes:@{NSForegroundColorAttributeName:RGBA(102, 102, 102, 1),NSFontAttributeName:font(12)} range:NSMakeRange(0, self.totalContentLb.text.length)];

    NSRange hourRang = [self.totalContentLb.text rangeOfString:[NSString stringWithFormat:@"%lu时",rs_hour]];
    NSRange minuteRang = [self.totalContentLb.text rangeOfString:[NSString stringWithFormat:@"%lu分",rs_minute]];

    if (hourRang.location != NSNotFound) {
        [attr3 addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:32]} range:NSMakeRange(hourRang.location, hourRang.length - 1)];
    }
    if (minuteRang.location != NSNotFound) {
        [attr3 addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:32]} range:NSMakeRange(minuteRang.location, minuteRang.length - 1)];
    }
    self.totalContentLb.attributedText = attr3;

        
        
        //        NSUInteger loc = 0;
//        NSUInteger len = [self.totalContentLb.text rangeOfString:@"时"].location - loc;
//
//
//        [attr3 addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:32] range:NSMakeRange(0, len)];

////        [attr3 setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:32]} range:rang];
//
////        [attr3 setAttributes:@{NSForegroundColorAttributeName:RGBA(102, 102, 102, 1),NSFontAttributeName:font(12)} range:NSMakeRange(self.totalContentLb.text.length - 1, 1)];
//        self.totalContentLb.attributedText = attr3;
    
  
    
//
//
}

@end
